<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Project: Vietsam
 * Package Controller
 * Class: Tab
 * Author: Khoi Pham		email: pmkhoi@gmail.com
 * Created on Dec 18, 2008
 * Updated on Dec 30, 2008
 *
 * */
class Tab extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('tab_model', 'tab', true);
        $this->load->model('counter_model', 'counter', true);
        //print_r($this->counter->show_counter());

        $this->checkLogin();
        //$this->checkPermission();

        $this->template->set_template('admin');
        $this->buildMenu();
    }

    function Tab() {
        parent::__construct();
        $this->load->library('xajax', 'category');
        $this->xajax->registerFunction(array('test', &$this, 'test'));

        $this->xajax->processRequest();
        $this->xajax_js = $this->xajax->getJavascript($this->base_url . 'js/');
        $this->load->model('tab_model', 'tab', true);
        $this->load->helper('form');
        $this->main = '';

        $this->load->model('counter_model', 'counter', true);
        //print_r($this->counter->show_counter());

        $this->checkLogin();
        $this->checkPermission();
    }

    function visitor() {
        $this->load->library('image_lib');
        $config['source_image'] = './images/03.jpg';
        $config['wm_text'] = 'Copyright 2006 - John Doe';
        $config['wm_type'] = 'text';
        $config['wm_font_path'] = './font/arial.ttf';
        $config['wm_font_size'] = '16';
        $config['wm_font_color'] = 'ffffff';
        $config['wm_vrt_alignment'] = 'bottom';
        $config['wm_hor_alignment'] = 'center';
        $config['wm_padding'] = '20';

        $this->image_lib->initialize($config);

        $this->image_lib->watermark();
    }

    function index() { //echo time();
        /* $error_message = '';
          $this->setTemplate(array('tabs_list.tpl'));
          $this->main = eval($this->loadTemplate('tabs_list.tpl'));
          $this->execute(); */
        $this->view();
    }

    function create() {
        $tabs = $this->tab->getTabs(0);

        $home = new stdClass();

        $home->id_tab = 0;
        $home->id_parent_tab = 0;
        $home->class_name = '';
        $home->vi_name = 'Parent';
        $home->position = -1;

        $tabs[] = $home;
        //echo $tab->id_parent;
        //$ddlLang = $this->buidDDL($langs, 'id_lang', $id_lang);
        $ddlParent = $this->buidDDL($tabs, 'id_parent', 0);

        $error_message = '';
        //$this->setTemplate(array (
        //'admin/edit_tab.tpl'
        //));
        //$this->main = eval ($this->loadTemplate('admin/edit_tab.tpl'));
        //$this->execute();

        $data = array();
        $data['id'] = 0;
        $data['vi_name'] = '';
        $data['en_name'] = '';
        $data['class_name'] = '';
        $data['ddlParent'] = $ddlParent;
        $data['error_message'] = '';
        $this->template->parse_view('content', 'admin/tab/edit_tab', $data, true);
        $this->template->render();
    }

    function edit($id_tab) {
        $tab = $this->tab->getById($id_tab);
        if (!$tab) {
            redirect('admin/tab');
            return;
        }

        $tabs = $this->tab->getTabs($id_tab);

        $home = new stdClass();

        $home->id_tab = 0;
        $home->id_parent_tab = 0;
        $home->class_name = '';
        $home->vi_name = 'Parent';
        $home->position = -1;

        $tabs[] = $home;
        //echo $tab->id_parent;
        //$ddlLang = $this->buidDDL($langs, 'id_lang', $id_lang);
        $ddlParent = $this->buidDDL($tabs, 'id_parent', $tab->id_parent_tab);
        
        $error_message = '';
        //$this->setTemplate(array (
        //'admin/edit_tab.tpl'
        //));
        //$this->main = eval ($this->loadTemplate('admin/edit_tab.tpl'));
        //$this->execute();

        $data = array();
        $data['id'] = $tab->id_tab;
        $data['vi_name'] = $tab->vi_name;
        $data['en_name'] = $tab->en_name;
        $data['class_name'] = $tab->class_name;
        $data['ddlParent'] = $ddlParent;
        $data['error_message'] = '';
        $this->template->parse_view('content', 'admin/tab/edit_tab', $data);
        $this->template->render();
       
    }

    function save() {
        $id = (int) $this->input->post('txtID');
        $tab = new stdClass();
        if ($id > 0) {
            $tab = $this->tab->getById($id);
            if (!$tab) {
                redirect('admin/tab');
                return;
            }
        } else {
            $tab->id_tab = 0;
            $tab->icon = '';
        }

        $tab->vi_name = trim($this->input->post('txtViName'));
        $tab->en_name = trim($this->input->post('txtEnName'));
        $tab->class_name = trim($this->input->post('txtControl'));
        $tab->id_parent_tab = trim($this->input->post('id_parent'));


        $error = false;
        $error_message = '';

        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('txtViName', 'lang:vi_name', 'required|min_length[2]|max_length[20]');
        $this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[2]|max_length[20]');
        $this->form_validation->set_rules('txtControl', 'Control', 'required|min_length[1]|max_length[30]');

        $attach_image = false;
        $image_upload = array('error' => false);
        if (isset($_FILES['file']['tmp_name']) AND strlen($_FILES['file']['tmp_name']) > 0) {
            $attach_image = true;
            $image_upload = $this->upload_image('file', ICON_PATH);
            if ($image_upload['error'])
                $error_message .= $image_upload['message'];
        }

        if ($this->form_validation->run() == FALSE OR ($attach_image == true AND $image_upload['error'] == true)) {
            $tabs = $this->tab->getTabs($id);
            $home = new stdClass();
            $home->id_tab = 0;
            $home->id_parent_tab = 0;
            $home->class_name = 'home';
            $home->vi_name = 'Home';
            $tabs[] = $home;
            $ddlParent = $this->buidDDL($tabs, 'id_parent', $tab->id_parent_tab);

            //$error_message = '';

            $data = array();
            $data['id'] = $tab->id_tab;
            $data['vi_name'] = $tab->vi_name;
            $data['en_name'] = $tab->en_name;
            $data['class_name'] = $tab->class_name;
            $data['ddlParent'] = $ddlParent;
            $data['error_message'] = '<div style="color:red">' . validation_errors() . '<br>' . $error_message . '</div>';

            $data['legend_text'] = $id > 0 ? lang('edit_product') : lang('create_product');
            $this->template->parse_view('content', 'admin/tab/edit_tab', $data);
            $this->template->render();
        } else {
            if ($attach_image) {
                //print_r($image_upload);
                $file_path = ICON_PATH . $image_upload['upload_data']['file_name'];
                if ($id > 0 AND $tab->icon != $file_path) {
                    @unlink($tab->icon);
                }

                $tab->icon = $file_path;
            }

            if ($id > 0) {
                $result = $this->tab->update($tab);
            } else {
                $this->tab->id_parent_tab = $tab->id_parent_tab;
                $this->tab->vi_name = $tab->vi_name;
                $this->tab->en_name = $tab->en_name;
                $this->tab->position = $this->tab->getMaxPosition() + 1;
                $this->tab->class_name = $tab->class_name;
                $this->tab->icon = $tab->icon;

                $id_tab = $this->tab->insert();
                $this->load->model('profile_model', 'profile', true);
                $profiles = $this->profile->getAll();
                $id_profiles = array();
                for ($i = 0; $i < count($profiles); $i++) {
                    $id_profiles[] = $profiles[$i]['id_profile'];
                }

                if (count($id_profiles) > 0) {
                    $this->load->model('access_model', 'access', true);
                    $this->access->insert($id_profiles, $id_tab);
                }
            }

            redirect('admin/tab/');
        }
    }

    function delete() {
        $id = intval($this->uri->segment(4));
        $ids = array();
        if ($id > 0) {
            $ids[] = (int) $id;
            //$this->product->delete($id);
        } else {
            if (($this->input->post('submitDelcategory'))) {
                $ids = $this->input->post('categoryBox');
            }
        }

        if (count($ids)) {

            $result = $this->tab->delete($ids);
        }

        redirect('admin/tab/');
    }

    function remove($id_tab) {
        $id_lang = 1; // Tmp

        $this->load->model('tab_model', 'tab', true);
        $tab = $this->tab->getById($id_tab, $id_lang);
        $tabs = $this->tab->getAll($id_lang, 0);
        //$tabs = $tabs[0]->control;
        $home = new stdClass();

        $home->id_tab = 0;
        $home->control = '';
        $home->name = 'Home';

        $tabs[] = $home;

        /* for($i = 0; $i < count($tabs); $i++){
          $label = $tabs['name'];
          $value = $tabs['id_tab'];
          $selected = '';
          $options .= eval($this->loadTemplate('option_ddl.tpl'));
          } */

        $ddlParent = $this->buidDDL($tabs, 'id_parent', $id_tab);

        $object = new xajaxResponse();
        //$object->addAlert("All done");
        $this->setTemplate(array(
            'edit_tab.tpl'
        ));
        $name = $tab->name;
        $data = eval($this->loadTemplate('edit_tab.tpl'));
        $object->assign('vietsam', 'innerHTML', $data);
        return $object;
    }

    function view() {
        $NUMBER_RECORD_PER_PAGE = 15;
        $this->load->library('pagination');
        $parent = $this->uri->segment(4, 0);
        $page = (int) $this->uri->segment(5);
        $config['base_url'] = site_url('admin/tab/view/' . $parent);
        $config['total_rows'] = $this->tab->getAllCount($parent);
        $config['per_page'] = $NUMBER_RECORD_PER_PAGE;
        $config['uri_segment'] = 5;
        $config['first_link'] = 'First';

        $this->pagination->initialize($config);

        $rows = $this->tab->getAll($parent, $page, $NUMBER_RECORD_PER_PAGE);
        $data = array();
        $data['rows'] = $rows;

        $this->template->parse_view('content', 'admin/tab/tabs_list', $data);
        $this->template->render();
        //$this->main = $this->load->view('admin/tab/tabs_list', $data, true);

        //$this->execute();
    }

    function change_position() {
        $id1 = (int) $this->uri->segment(4);
        $id2 = (int) $this->uri->segment(5);

        $tab1 = $this->tab->getById($id1);
        $tab2 = $this->tab->getById($id2);

        if ($tab1 AND $tab2) {
            $position1 = $tab1->position;
            $tab1->position = $tab2->position;
            $tab2->position = $position1;

            $this->tab->update($tab1);
            $this->tab->update($tab2);
        }

        redirect('admin/tab/');
    }

    function buidDDL($items, $name, $default_value, $action = '', $style = '', $class = '') {
        
        $options = '';
        $space = '';
        for ($i = 0; $i < count($items); $i++) {
            $label = $items[$i]->vi_name;
            $value = $items[$i]->id_tab;
            if ($value == $default_value)
                $selected = 'selected';
            else
                $selected = '';
            //$options .= eval($this->loadTemplate('admin/option_ddl.tpl'));
            $options .= $this->load->view('admin/option_select', array('value' => $value, 'selected' => $selected, 'space' => $space, 'label' => $label), true);
        }
        $data['name'] = $name;
        $data['action'] = $action;
        $data['style'] = $style;
        $data['class'] = $class;
        $data['options'] = $options;

        return $this->load->view('admin/select', $data, true);
        //return eval($this->loadTemplate('admin/ddl.tpl'));
    }

    function upload_image($field_name, $path) {
        $result = array();
        $config['upload_path'] = $path; //'./Data/Images/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        //$config['max_size']	= '1000';
        $config['max_width'] = '30';
        $config['max_height'] = '30';
        $config['overwrite'] = true;


        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($field_name)) {
            //$error = array('error' => $this->upload->display_errors());
            $result['error'] = true;
            $result['message'] = $this->upload->display_errors();
            //$this->load->view('upload_form', $error);
        } else {
            //$data = array('upload_data' => $this->upload->data());
            $result['error'] = false;
            $result['upload_data'] = $this->upload->data();
        }

        return $result;
    }
    
}
?>
